package com.handydata.antifraud.web;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Objects;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.handydata.antifraud.bo.rejectprovincebasedata.RiskRuleDictBasedataResBO;
import com.handydata.antifraud.common.exception.RiskRuleDictInfoExCode;
import com.handydata.antifraud.entity.RiskRuleDictIinfoDao;
import com.handydata.antifraud.service.RiskRuleDictInfoService;
import com.handydata.antifraud.vo.appcategorybasedata.RiskRuleDictInfoResListVO;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.web.AbsController;

@RequestMapping("/riskRuleConfig")
@RestController
@Slf4j
public class RiskRuleDictlController extends AbsController {
	
	@Autowired
	private RiskRuleDictInfoService riskRuleDictInfoService;
	
	@ApiOperation(value="新增规则文件刷新", notes="新增规则文件刷新")
	@ApiImplicitParam(name = "req", value = "新增规则文件刷新", required = true, dataType = "CreateAppCategoryBasedataReqVO", paramType = "body")
	@PostMapping("/insert")
	public RiskRuleDictInfoResListVO insert(@RequestBody RiskRuleDictBasedataResBO req) {
		try {
			log.info("Execute RiskRuleDictInfoResListVO, param is {}", req);
			RiskRuleDictInfoResListVO riskRuleDictInfoResListVO = new RiskRuleDictInfoResListVO();
			if(Objects.isNull(req)){
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PROCESSNAME.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("RiskRuleDictBasedataResBO入参为空");
				return riskRuleDictInfoResListVO;
			}
			String processName = req.getProcessName();
			String productCode = req.getProductCode();
			String custTypeCode = req.getCustTypeCode();
 			String packageName = req.getPackageName();
			String status = req.getStatus();
			String processDecisionScenario = req.getProcessDecisionScenario();
			if(StringUtils.isBlank(processName)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PROCESSNAME.getErrorMsg()+"= {}",processName);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PROCESSNAME.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PROCESSNAME.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			if(StringUtils.isBlank(productCode)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PRODUCTCODE.getErrorMsg()+"= {}",productCode);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PRODUCTCODE.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PRODUCTCODE.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			log.info("(客户类型编号)custTypeCode = {}",custTypeCode);
			if(StringUtils.isBlank(packageName)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PRODUCTCODE.getErrorMsg()+"= {}",productCode);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_CUSTTYPECODE.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_CUSTTYPECODE.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			if(StringUtils.isBlank(status)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS.getErrorMsg()+"= {}",productCode);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			if(status.length() > 1){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS_FOMAT.getErrorMsg()+"= {}",productCode);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS_FOMAT.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS_FOMAT.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			if(StringUtils.isBlank(processDecisionScenario)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_SCENARIO.getErrorMsg()+"= {}",productCode);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_SCENARIO.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_SCENARIO.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			if(processDecisionScenario.length() > 1){
				log.info("授信续约区别码为: ",processDecisionScenario);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_SCENARIO_FOMAT.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_SCENARIO_FOMAT.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			RiskRuleDictIinfoDao riskRuleDictIinfoDao = riskRuleDictInfoService.queryRiskRuleDictInfoDao(req.getPackageName());
			if("3".equals(riskRuleDictIinfoDao.getStatus())){
				riskRuleDictIinfoDao = CommonBeanConvertor.convert(req, RiskRuleDictIinfoDao.class);
				riskRuleDictInfoService.updateRiskRuleDictInfoDao(riskRuleDictIinfoDao);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SUCCESS.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
			if(Objects.isNull(riskRuleDictIinfoDao)){
				riskRuleDictIinfoDao = CommonBeanConvertor.convert(req, RiskRuleDictIinfoDao.class);
				riskRuleDictInfoService.insertRiskRuleDictInfoDao(riskRuleDictIinfoDao);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg(RiskRuleDictInfoExCode.SUCCESS.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}else{
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.ERROR.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("产品包为:"+req.getPackageName()+"名称"+RiskRuleDictInfoExCode.ERROR.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
		} catch (Exception e) {
			return handleException(e, RiskRuleDictInfoResListVO.class, null, RiskRuleDictInfoExCode.SYS_QUERY_NETWORK_ERROR);
		}
	}

	@ApiOperation(value="修改规则文件刷新", notes="修改规则文件刷新") 
	@ApiImplicitParam(name = "req", value = "修改规则文件刷新", required = true, dataType = "ModifyAppCategoryBasedataReqVO", paramType = "body")
	@PostMapping("/modify")
	public RiskRuleDictInfoResListVO modify(@RequestBody RiskRuleDictBasedataResBO req) {
		try {
			log.info("Execute modify, param is ", req);
			RiskRuleDictInfoResListVO riskRuleDictInfoResListVO = new RiskRuleDictInfoResListVO();
			if(Objects.isNull(req)){
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PROCESSNAME.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("请给个要删除的产品包");
				return riskRuleDictInfoResListVO;
			}
			String packageName = req.getPackageName();

			String status = req.getStatus();

			if(StringUtils.isBlank(packageName)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PRODUCTCODE.getErrorMsg()+"= {}",packageName);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_CUSTTYPECODE.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("请给我一个要删除的产品包!");
				return riskRuleDictInfoResListVO;
			}

			if(StringUtils.isBlank(status)){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PRODUCTCODE.getErrorMsg()+"= {}",packageName);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("请给我一个正确的状态码");
				return riskRuleDictInfoResListVO;
			}

			if(Integer.valueOf(status) > 3){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUSCODE.getErrorMsg()+"= {}",packageName);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUSCODE.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("目前系统中支持状态码为：0-3范围,请做修改");
				return riskRuleDictInfoResListVO;
			}

			if(Integer.valueOf(status) < 0){
				log.info(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUSCODE.getErrorMsg()+"= {}",packageName);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SYS_LOAD_RULETO_STATUSCODE.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("目前系统中支持状态码为：0-3范围,请做修改");
				return riskRuleDictInfoResListVO;
			}
			RiskRuleDictIinfoDao riskRuleDictIinfoDao = riskRuleDictInfoService.queryRiskRuleDictInfoDao(req.getPackageName());

			if(Objects.isNull(riskRuleDictIinfoDao)){
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("产品包为:"+req.getPackageName()+"已经被删除");
				return riskRuleDictInfoResListVO;
			}else{
				riskRuleDictIinfoDao.setStatus(status);
				riskRuleDictInfoService.updateRiskRuleDictInfoDao(riskRuleDictIinfoDao);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("产品包为:"+req.getPackageName()+"名称，关闭"+RiskRuleDictInfoExCode.SUCCESS.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}
		} catch (Exception e) {
			return handleException(e, RiskRuleDictInfoResListVO.class, req, RiskRuleDictInfoExCode.SYS_LOAD_RULETO_PROCESSNAME);
		}
	}

	@ApiOperation(value="删除规则文件刷新", notes="删除规则文件刷新")
	@ApiImplicitParam(name = "req", value = "删除规则文件刷新", required = true, dataType = "DeleteAppCategoryBasedataReqVO", paramType = "body")
	@PostMapping("/remove")
	public RiskRuleDictInfoResListVO deleteAppCategoryBasedata(@RequestBody RiskRuleDictBasedataResBO req) {
		try {
			RiskRuleDictInfoResListVO riskRuleDictInfoResListVO = new RiskRuleDictInfoResListVO();
			log.info("Execute deleteRiskRuleDictlController, param is {}", req);
			RiskRuleDictIinfoDao riskRuleDictIinfoDao = riskRuleDictInfoService.queryRiskRuleDictInfoDao(req.getPackageName());
			if(Objects.isNull(riskRuleDictIinfoDao)){
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("产品包为:"+req.getPackageName()+"已经被删除");
				return riskRuleDictInfoResListVO;
			}else if(!"3".equals(riskRuleDictIinfoDao.getStatus())){
				riskRuleDictIinfoDao.setStatus("3");
				riskRuleDictInfoService.updateRiskRuleDictInfoDao(riskRuleDictIinfoDao);
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("产品包为:"+req.getPackageName()+"名称，删除"+RiskRuleDictInfoExCode.SUCCESS.getErrorMsg());
				return riskRuleDictInfoResListVO;
			}else{
				riskRuleDictInfoResListVO.setRespCode(RiskRuleDictInfoExCode.SUCCESS.getErrorCode());
				riskRuleDictInfoResListVO.setRespMsg("产品包为:"+req.getPackageName()+"已经被删除");
				return riskRuleDictInfoResListVO;
			}
		} catch (Exception e) {
			return handleException(e, RiskRuleDictInfoResListVO.class, req, RiskRuleDictInfoExCode.SYS_UNKNOWN_ERROR);
		}
	}
	
	@ApiOperation(value="查询规则文件刷新", notes="查询规则文件刷新")
	@ApiImplicitParam(name = "req", value = "查询规则文件刷新", required = true, dataType = "RiskRuleDictInfoResVO", paramType = "body")
	@PostMapping("/query")
	public RiskRuleDictInfoResListVO queryAppCategoryBasedata() {
		try {
			log.info("Execute RiskRuleDictInfoBaseData, param is");
			List<RiskRuleDictIinfoDao> res = riskRuleDictInfoService.queryRiskRuleDictInfoDaoList();
			RiskRuleDictInfoResListVO resVO = CommonBeanConvertor.convert(res, RiskRuleDictInfoResListVO.class, RiskRuleDictBasedataResBO.class);
			resVO.setRespCode("0000");
			resVO.setRespMsg("查询成功");
			return resVO;
		} catch (Exception e) {
			return handleException(e, RiskRuleDictInfoResListVO.class, null, RiskRuleDictInfoExCode.SYS_QUERY_NETWORK_ERROR);
		}
	}
}
